﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using AutoMapper;
using Jinjia.Approve.Entity.Entity.JJFlow;
using Jinjia.Approve.IRepository.JJFlow;
using Jinjia.Approve.IService.JJFlow;
using Jinjia.Approve.Model.Input.JJFlow;
using Jinjia.Approve.Model.VM;
using Jinjia.Approve.Model.VM.JJFlow;
using Jinjia.Core.Attribute;
using Jinjia.Core.BaseInfrastructure.BaseModel.BaseModel;
using Jinjia.Core.BaseInfrastructure.StorageModel.Model;
using Microsoft.AspNetCore.Http;

namespace Jinjia.Approve.Service.JJFlow
{
    public class JJFlowDicCodeService : JJFlowCommonService, IJJFlowDicCodeService
    {
        private readonly IFlowDicCodeRepository _flowDicCodeRepository;
        private readonly IMapper _mapper;

        public JJFlowDicCodeService(IHttpContextAccessor httpContextAccessor,
            IJJFlowClientService jjFlowClientService,
            IFlowDicCodeRepository flowDicCodeRepository,
            IMapper mapper) : base(httpContextAccessor,
            jjFlowClientService)
        {
            _flowDicCodeRepository = flowDicCodeRepository;
            _mapper = mapper;
        }

        public async Task<PageListSearchResultVM<JJDictCodeViewModel>> GetMappingPageList(JJDictCodeSearchCondition searchCondition)
        {
            return await _flowDicCodeRepository.GetMappingPageList(searchCondition, GetUserId());
        }

        public async Task<JjFlowDictcodeVM> GetById(int id)
        {
            var jjFlowDictcode = await _flowDicCodeRepository.GetById(id);
            var jjFlowDictcodeVm = _mapper.Map<JjFlowDictcodeVM>(jjFlowDictcode);
            return jjFlowDictcodeVm;
        }

        public async Task<List<JjFlowDictcodeVM>> GetListByTemplateId(int templateId) => _mapper.Map<List<JjFlowDictcodeVM>>(await _flowDicCodeRepository.GetListByTemplateId(templateId));

        [UseTran]
        public virtual async Task<ResponseResult<string>> SaveDictCode(JJDictCodeEditModel model)
        {
            var dictCodeCount = await _flowDicCodeRepository.GetDicCodeCount(model.dictcode_key, model.id, model.template_id);
            if (dictCodeCount > 0)
            {
                return new ResponseResult<string>()
                {
                    IsSuccess = false,
                    Message = "变量key已存在"
                };
            }

            var dictCodes = await _flowDicCodeRepository.GetListByTemplateId(model.template_id);
            var pushFeiShuDictCodes = dictCodes.Where(x => x.is_push_feishu).Select(x => x.dictcode_key).ToHashSet();
            if (model.is_push_feishu)
            {
                if (!model.sort_number.HasValue)
                {
                    return new ResponseResult<string>()
                    {
                        IsSuccess = false,
                        Message = "当推送飞书时,排序号为必填项"
                    };
                }
                pushFeiShuDictCodes.Add(model.dictcode_key);
            }

            if (pushFeiShuDictCodes.Count > 3)
            {
                return new ResponseResult<string>()
                {
                    IsSuccess = false,
                    Message = "最多只能设置三个推送飞书字段"
                };
            }

            var dictcode = new jj_flow_dictcode
            {
                id = model.id
            };
            if (dictcode.id == 0)
            {
                dictcode.deleted = 0;
                dictcode.template_id = model.template_id;
                dictcode.create_user_id = dictcode.update_user_id = GetUserId();
                dictcode.create_time = dictcode.update_time = DateTime.Now;
            }
            else
            {
                dictcode = await _flowDicCodeRepository.GetById(dictcode.id);
                if (dictcode == null)
                {
                    return new ResponseResult<string>()
                    {
                        IsSuccess = false,
                        Message = "变量id不存在"
                    };
                }
            }

            dictcode.dictcode_name = model.dictcode_name;
            dictcode.dictcode_key = model.dictcode_key;
            dictcode.is_push_feishu = model.is_push_feishu;
            dictcode.sort_number = model.sort_number;
            dictcode.remark = model.remark;
            dictcode.update_user_id = GetUserId();

            await _flowDicCodeRepository.Save(dictcode);
            return new ResponseResult<string>()
            {
                IsSuccess = true
            };
        }

        [UseTran]
        public virtual async Task<ResponseResult<string>> DeleteDictCode(int id)
        {
            var dictcode = await _flowDicCodeRepository.GetById(id);
            if (dictcode != null)
            {
                await _flowDicCodeRepository.LogicalDelete(id, GetUserId());
                return new ResponseResult<string>(){
                    IsSuccess = true,
                    Message = "删除成功"
                };
            }
            else
            {
                return new ResponseResult<string>()
                {
                    IsSuccess = false,
                    Message = "变量不存在"
                };
            }
        }
    }
}